<script setup lang="ts">
import { onMounted, ref } from "vue-demi";
import { getSongDetail, getSongUrlNew } from "@/api/content";
import { useRoute } from "vue-router";
import { usePlaySong } from "@/hooks/usePlaySong";
import { getAuth } from "@/utils/util";

const { params }: any = useRoute();

const posInfo = ref<any>({});
const audioRef = ref<HTMLAudioElement | null>(null);
const lrcUl = ref<HTMLElement | null>(null);
const containerRef = ref<HTMLElement | null>(null);

const { data, isPlay, currentIndex, offsetY } = usePlaySong({
  audioRef,
  lrcContainerRef: lrcUl,
  containerRef,
});

onMounted(async () => {
  // 获取歌曲详情
  const res = await getSongDetail(params.id);
  posInfo.value = res.songs[0];
  // 获取歌曲
  const { data } = await getSongUrlNew(params.id);
  posInfo.value.song = {
    time: data[0].time,
    url: data[0].url,
  };
});
</script>
<!-- 歌曲详情 -->
<template>
  <div class="m-song-container">
    <div
      class="m-song-bg"
      :style="{
        backgroundImage: `url('${posInfo.al?.picUrl}')`,
        opacity: 1,
        transform: 'scale(1.5)',
        transformOrigin: 'center center',
        filter: 'blur(60px)',
      }"
    ></div>
    <div class="mask"></div>
    <div class="m-song-content">
      <div class="m-song-wrapper">
        <div class="m-song-disc">
          <div class="m-song-turn">
            <div class="m-song-rollup">
              <div
                class="m-song-img"
                :style="{ animationPlayState: isPlay ? 'running' : 'paused' }"
              >
                <img class="u-img" :src="posInfo.al?.picUrl" alt="" />
              </div>
            </div>
            <div class="m-song-playbtn" v-show="!isPlay"></div>
          </div>
        </div>
        <div class="m-song-lrc">
          <h2 class="m-song-title">
            <span class="m-song-name"
              >{{ posInfo.name
              }}{{ posInfo.tns ? `(${posInfo.tns?.join(",")})` : "" }}</span
            >
            <span class="m-song-gap"> - </span>
            <span class="m-song-auth">{{ getAuth(posInfo.ar) }}</span>
          </h2>
          <div ref="containerRef" class="lrc-wrapper">
            <ul
              ref="lrcUl"
              class="lrc-list"
              :style="{ transform: `translateY(-${offsetY}px)` }"
            >
              <li
                class="lrc-item"
                v-for="(item, index) in data"
                :key="'lrc' + index"
                :class="{ active: currentIndex === index }"
              >
                {{ item.words }}
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
    <audio
      ref="audioRef"
      autoplay
      controls
      style="width: 0; height: 0; display: none"
      :src="posInfo.song?.url"
    ></audio>
  </div>
</template>

<style lang="scss" scoped>
@import "@/assets/styles/mixin";
.m-song-container {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  min-width: 640px;
  overflow: hidden;

  .m-song-bg {
    position: fixed;
    left: 0;
    right: 0;
    top: 0;
    height: 100%;
    overflow: hidden;
    z-index: 1;
    background-color: #161824;
    background-position: 50%;
    background-repeat: no-repeat;
    background-size: auto 100%;
  }

  .mask {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 1;
    background-color: rgba(0, 0, 0, 0.4);
  }

  .m-song-content {
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;
    z-index: 2;

    .m-song-wrapper {
      display: flex;
      flex-direction: column;
      height: 100%;
      padding-top: 140px;
    }
  }
}

.m-song-disc {
  position: relative;
  width: 500px;
  height: 500px;
  margin: 0 auto;

  &::after {
    content: "";
    position: absolute;
    top: -100px;
    left: 224px;
    width: 148px;
    height: 236px;
    z-index: 3;
    background: url("@/assets/images/turn-line.png") no-repeat;
    background-size: contain;
    transform: rotate(-9deg);
    transform-origin: left top;
  }

  .m-song-turn {
    width: 100%;
    height: 100%;

    &::before {
      content: "";
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      z-index: 2;
      background: url("@/assets/images/turn-out.png") no-repeat;
      background-size: contain;
    }
  }

  .m-song-rollup {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 300px;
    height: 300px;
    margin-top: -150px;
    margin-left: -150px;
    overflow: hidden;

    .m-song-img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      overflow: hidden;
      animation: spin 20s linear infinite;
    }
  }

  .m-song-playbtn {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 2;
    width: 100px;
    height: 100px;
    background: url("@/assets/images/play.png") 0 0 no-repeat;
    background-size: contain;
  }
}

.m-song-lrc {
  flex: 1;
  overflow: hidden;
  margin: 40px 0;
  padding: 0 40px;

  .m-song-title {
    font-size: 36px;
    text-align: center;
    line-height: 1.1;
    color: #fefefe;
    @include in1line;

    .m-song-auth {
      font-size: 32px;
      color: rgba(255, 255, 255, 0.6);
    }
  }
}

.lrc-wrapper {
  height: 100%;
  margin-top: 20px;
  overflow: hidden;

  .lrc-list {
    transition: 0.6s;
  }

  .lrc-item {
    height: 60px;
    line-height: 60px;
    text-align: center;
    color: rgba(255, 255, 255, 0.8);
    transition: 0.2s;
    @include in1line;

    .m-song-gap {
      margin: 0 10px;
    }

    &.active {
      color: rgba(255, 255, 255, 1);
    }
  }
}
</style>
